package com.hgw.easyexcel3_demo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.data.ImageData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.hgw.easyexcel3_demo.domain.PlaceListDetailDownloadData;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author Linhuibq-YanAn
 * @date 2022/10/20 17:59
 */
@SpringBootTest
public class WritePlaceList {

    private final String PATH = "/Users/gwh/Documents/data/demo/easy-excel-demo/easyexcel3_demo/src/main/resources/warehouse/";

    @Test
    public void noModelWrite() {
        // 表名
        String fileName = PATH + "placeList" + System.currentTimeMillis() + ".xlsx";
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName, PlaceListDetailDownloadData.class)
                .registerWriteHandler(horizontalCellStyleStrategy)
                .sheet("场地清单").doWrite(data());
    }

    private List<PlaceListDetailDownloadData> data() {
        List<PlaceListDetailDownloadData> dataList = new ArrayList<>();
        PlaceListDetailDownloadData data1 = new PlaceListDetailDownloadData();
        data1.setId(1113L);
        data1.setPlaceName("新华广场");
        data1.setSpotLoc("一楼大厅");
        data1.setCity("石家庄市");
        data1.setAddress("河北省石家庄市新乐市长寿街道新华广场信誉楼百货(新乐店)");
        data1.setDistrict("新乐市");
        data1.setPlaceCategoryName("商场");
        data1.setProjectLocation("中低端");
        data1.setSpotSize("3*3");
        data1.setBrand("19八3");
        data1.setWeekdaysFlow(BigDecimal.valueOf(1111L));
        data1.setWeekendFlow(BigDecimal.valueOf(2222));
        data1.setWeekdaysPrice(BigDecimal.valueOf(234));
        data1.setQuote(BigDecimal.valueOf(234.0));
        data1.setBearingInfo("2.33");

        WriteCellData<Void> writeCellData1 = new WriteCellData<>();
        List<ImageData> imageDataList1 = new ArrayList<>();
        ImageData imageData1 = null;
        for (int i = 0; i < 10; i++) {
            imageData1 = new ImageData();
            imageData1.setImage(getUrlFileData("https://img.linhuiba.com/FvXO_WtI7CI2z6-kx2BbD93fNdWS-linhuiba_watermark"));
            imageData1.setImageType(ImageData.ImageType.PICTURE_TYPE_PNG);
            imageData1.setTop(2);
            imageData1.setBottom(2);
            imageData1.setLeft(2 + i * 71);
            imageData1.setRight((11 - i) * 71);
            imageDataList1.add(imageData1);
        }
        writeCellData1.setImageDataList(imageDataList1);
        data1.setPlacePicture(writeCellData1);
        data1.setSpotPicture(writeCellData1);
        data1.setFloorPicture(writeCellData1);

        dataList.add(data1);
        dataList.add(data1);

        return dataList;
    }

    public static byte[] getUrlFileData(String fileUrl) {
        try {
            URL url = new URL(fileUrl);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            httpConn.connect();
            InputStream cin = httpConn.getInputStream();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = cin.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            cin.close();
            byte[] fileData = outStream.toByteArray();
            outStream.close();
            return fileData;
        } catch (IOException e) {
            e.printStackTrace();
            return new byte[1024];
        }
    }

}
